package com.tanxi521.tnweb.service.impl;

import com.tanxi521.tnweb.mapper.PlayerAccountMapper;
import com.tanxi521.tnweb.mapper.WebUserMapper;
import com.tanxi521.tnweb.model.AllUserInfo;
import com.tanxi521.tnweb.model.PlayerAccount;
import com.tanxi521.tnweb.model.WebUser;
import com.tanxi521.tnweb.service.WebUserService;
import com.tanxi521.tnweb.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class WebUserServiceImpl implements WebUserService {

    @Autowired
    WebUserMapper webUserMapper;

    @Autowired
    PlayerAccountMapper playerAccountMapper;

    @Override
    public Integer checkId(String userId) {
        return webUserMapper.selectUserCount(userId);
    }

    @Override
    public Integer register(String userId, String password) {
        WebUser webUser = new WebUser();
        Date date = new Date();
        webUser.setUserId(userId);
        webUser.setUserPassword(password);
        webUser.setRegTime(date);
        webUser.setIsBound(0);
        int i = new Random().nextInt(5) + 1;
        webUser.setHeadImg("/tnweb/image/user_head/default_head" + i + ".png");
        return webUserMapper.insert(webUser);
    }

    @Override
    public int getWebUserNums() {
        return webUserMapper.selectAllUserNums();
    }

    @Override
    public WebUser queryUsersByUserIdAndPassword(String userId) {
        return webUserMapper.selectUserByUserId(userId);
    }

    @Override
    public void modifyUserLastLoginTime(Date date, String userId) {
        webUserMapper.updateUserLastLoginTimeByUserId(date, userId);
    }

    @Override
    public WebUser queryUserByUserId(Integer id) {
        return webUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AllUserInfo> queryAllUsers(PageInfo pageInfo) {
        Long i = pageInfo.getCunPage() - 1 < 0 ? 0 : pageInfo.getCunPage() - 1;
        Long start = i * pageInfo.getPageSize();
        Integer pageSize = pageInfo.getPageSize();
        List<AllUserInfo> allUserInfos = new ArrayList<>();
        List<WebUser> webUsers = webUserMapper.selectWebUserByStartAndSize(start, pageSize);
        for (WebUser webUser : webUsers) {
            AllUserInfo allUserInfo = new AllUserInfo();
            allUserInfo.setUserId(webUser.getId().toString());
            allUserInfo.setUserHead(webUser.getHeadImg());
            allUserInfo.setUserName(webUser.getUserId());
            PlayerAccount playerAccount = playerAccountMapper.selectByPrimaryKey(webUser.getMcId());
            if (playerAccount == null) {
                allUserInfo.setUserMcId("用户未绑定");
                allUserInfo.setUserIsOnline("游戏离线");
            } else {
                allUserInfo.setUserMcId(playerAccount.getRealname());
                Short islogged = playerAccount.getIslogged();
                if (islogged == 0) {
                    allUserInfo.setUserIsOnline("游戏离线");
                } else {
                    allUserInfo.setUserIsOnline("游戏在线");
                }
            }
            allUserInfos.add(allUserInfo);
        }
        return allUserInfos;
    }

    @Override
    public int modifyWebUser(WebUser webUser) {
        return webUserMapper.updateByPrimaryKey(webUser);
    }
}
